package com.htd.erp.validation;

import java.util.regex.Pattern;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.commons.lang3.StringUtils;

/**
 * @author Zhang Guilin
 */
public class BlankablePatternConstraintValidator implements ConstraintValidator<BlankablePattern, String>{
	
	private Pattern pattern;
	
	@Override
	public void initialize(BlankablePattern nullablePattern) {
		pattern = Pattern.compile(nullablePattern.regexp());
	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
		return StringUtils.isBlank(value) || pattern.matcher(value).matches();
	}

}
